﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace ArchiveIISLogs
{
    class Program
    {

        static Compressor logFileCompressor = new Compressor();

        static void Main(string[] args)
        {
            //  1.  Zip up any text log files that are on disk in the specified directories 
            List<FileInfo> filesAffected = CompressOldLogFiles();

            //  2. Delete any files in Step 1.
            DeleteRawLogFiles(filesAffected);

            //  3. Remove any logs from the zips that are older than X days        
            DeleteOldLogsInZip();
        }

        private static void DeleteOldLogsInZip()
        {
            //remove old items from the zip
            DateTime olderThan = DateTime.Today.AddDays(-1 * Config.GetRetensionInDays());
            LogFileDirectory lf = new LogFileDirectory();

            List<FileInfo> zipsToInspect = lf.GetZipFilesInWebLogDirectory(olderThan);
            Compressor logFileCompressor = new Compressor();

            //within all the archive zips: find and delete any logs that is older than our threshold date
            zipsToInspect.ForEach(x=>logFileCompressor.RemoveLogsInZipOlderThan(x.FullName, olderThan));

            AppLogger.LogMessage("{0} zip files were inspected. Removed any logs within them that were older than {1}".FormatWith(zipsToInspect.Count, olderThan.ToShortDateString()));
        }

        private static void DeleteRawLogFiles(List<FileInfo> logsToDelete)
        {
            LogFileDirectory lf = new LogFileDirectory();

            //since they've been archived, delete
            logsToDelete.ForEach(x => lf.DeleteFile(x));

            AppLogger.LogMessage(logsToDelete.Count + " log text files deleted.");
        }

        private static List<FileInfo> CompressOldLogFiles()
        {
            LogFileDirectory lf = new LogFileDirectory();

            //gather all the log files that need to be compressed into the zip
            List<FileInfo> filesToCompress = lf.GetWebsiteLogfileDirectories();

            CompressFiles(filesToCompress);

            //write the status to the logger
            AppLogger.LogMessage(filesToCompress.Count + " log text files compressed.");

            return filesToCompress;
        }

        private static void CompressFiles(List<FileInfo> logFiles)
        {
            if (logFiles == null) return;
            //add each file to the corresponding zip file in the root directory        
            logFiles.ForEach(logFile => AddLogFileToZip(logFile));
        }

        private static void AddLogFileToZip(FileInfo oneFile)
        {
            string zipFileName = oneFile.DirectoryName + ".zip";
            //this will simply make a new file with same name .zip extension. i.e. W3SVC.zip
            logFileCompressor.AddLogFileToZip(zipFileName, oneFile);
        }
    }
}
